package com.lw.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lw.yygh.hosp.feign.CmnFeignClient;
import com.lw.yygh.hosp.repository.HospRepository;
import com.lw.yygh.hosp.service.HospService;
import com.lw.yygh.model.hosp.Department;
import com.lw.yygh.model.hosp.Hospital;
import com.lw.yygh.vo.hosp.HospitalQueryVo;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospServiceImpl implements HospService {
    @Autowired
    private HospRepository hospRepository;
    @Autowired
    private CmnFeignClient cmnFeignClient;

    @Override
    public void save(Map<String, Object> map) {
        //将参数map集合转换对象
        String jsonString = JSONObject.toJSONString( map );
        Hospital hospital = JSONObject.parseObject( jsonString, Hospital.class );
        //判断数据库是否存在数据
        Hospital hospitalExist =hospRepository.findByHoscode(hospital.getHoscode());
        //如果不存在，进行添加
        if (hospitalExist==null){
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospRepository.save(hospital);

        }else {
            //如果存在，进行更新
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospital.setId( hospitalExist.getId() );
            hospRepository.save(hospital);

        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospRepository.findByHoscode( hoscode );
    }

    //全部医院列表
    @Override
    public Page<Hospital> findHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable=PageRequest.of( page-1,limit );
        //创建条件匹配器 模糊查询
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher( ExampleMatcher.StringMatcher.CONTAINING ).withIgnoreCase( true );
        //将hospitalQueryVo转换成hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties( hospitalQueryVo, hospital);
        Example<Hospital> hospitalExample = Example.of( hospital ,matcher);
        Page<Hospital> hospitals = hospRepository.findAll( hospitalExample, pageable );
        for (Hospital item : hospitals.getContent()){
            //对每个对象进行医院类型封装
            String name = cmnFeignClient.getName( "Hostype", item.getHostype() );
            //对每个对象进行省市区封装
            String province = cmnFeignClient.getName( item.getProvinceCode() );
            String city = cmnFeignClient.getName( item.getCityCode() );
            String district = cmnFeignClient.getName( item.getDistrictCode() );
            item.getParam().put( "hosTypeString",name );
            item.getParam().put( "addressString",province+city+district );
        }
        return hospitals;
    }

    //更新医院上线状态
    @Override
    public void updateHospStatus(String id, Integer status) {
        //先查后更
        Hospital hospital = hospRepository.findById( id ).get();
        hospital.setStatus( status );
        hospRepository.save( hospital );
    }

    //医院详情
    @Override
    public Hospital findHospById(String id) {
        return hospRepository.findById( id ).get();
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospRepository.findByHoscode(hoscode);
        if(null != hospital) {
            return hospital.getHosname();
        }
        return "";
    }

    //根据医院名称进行模糊查询 返回list集合 有多个
    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospRepository.findByHosnameLike(hosname);
    }

    //根据医院编号查询医院详情
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.getByHoscode(hoscode);
        //对对象进行医院类型封装
        String name = cmnFeignClient.getName( "Hostype", hospital.getHostype() );
        //对对象进行省市区封装
        String province = cmnFeignClient.getName( hospital.getProvinceCode() );
        String city = cmnFeignClient.getName( hospital.getCityCode() );
        String district = cmnFeignClient.getName( hospital.getDistrictCode() );
        hospital.getParam().put( "hosTypeString",name );
        hospital.getParam().put( "addressString",province+city+district );
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;

    }

}
